using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace url2jpeg
{
    public partial class settings : LocalizedUserControl
    {
        lang m_lang;
        userSettings m_userSettings;

        public delegate void LanguageChangedHandler(string newLang);
        public event LanguageChangedHandler LanguageChangedEvent;

        public settings()
        {
            InitializeComponent();
            m_userSettings = userSettings.GetInstance();
            if (KeyCorrect(m_userSettings.m_email, Convert.ToInt64(m_userSettings.m_regkey)))
                grpRegistration.Visible = false;
        }

        public static bool KeyCorrect(string key, Int64 data)
        {
            /*
            bool bResult = false;
            if (key != null && data != 0)
            {
                Int64 hash = key.GetHashCode();
                hash -= int.MinValue;
                bResult = (hash == data);
            }
            return bResult;
            */
            return true;
        }

        public void SetLang(lang l, string userSelection)
        {
            m_lang = l;
            cboLanguages.Items.Clear();
            int iuserSelection = -1;
            foreach (langCountry lc in l.m_locale)
            {
                int index = cboLanguages.Items.Add(lc.m_country);
                if (lc.m_country == userSelection)
                    iuserSelection = index;
            }
            cboLanguages.SelectedIndex = iuserSelection;
        }

        private void cboLanguages_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LanguageChangedEvent != null)
                LanguageChangedEvent(cboLanguages.SelectedItem.ToString());
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start(linkLabel1.Text);
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (KeyCorrect(txtEMail.Text, Convert.ToInt64(txtPassword.Text)))
                {
                    m_userSettings.m_email = txtEMail.Text;
                    m_userSettings.m_regkey = txtPassword.Text;
                    grpRegistration.Visible = false;
                }
            }
            catch { }
        }

        static public void LicenceChecking(Bitmap bmp)
        {
            userSettings us = userSettings.GetInstance();
            if (!KeyCorrect(us.m_email, Convert.ToInt64(us.m_regkey)))
            {
                Graphics gr = Graphics.FromImage(bmp);
                FontFamily fontFamily = new FontFamily("Arial");
                Font font = new Font(fontFamily, 24, FontStyle.Regular, GraphicsUnit.Pixel);
                string message = "Bitmap generated whith shareware url2jpeg (http://www.url2jpeg.com)";
                SizeF size = gr.MeasureString(message, font);
                for (int i = 0; i < bmp.Height; i += 150)
                {
                    gr.DrawString(message, font, Brushes.White, 0, i);
                    gr.DrawString(message, font, Brushes.Black, 1, i + 1);
                }
                for (int i = 75; i < bmp.Height; i += 150)
                {
                    gr.DrawString(message, font, Brushes.White, bmp.Width-size.Width, i);
                    gr.DrawString(message, font, Brushes.Black, bmp.Width - size.Width+1, i + 1);
                }
                gr.Dispose();
            }
        }
    }
}
